#ifndef UTILS_H
#define UTILS_H
#include "service.grpc.pb.h"
#include "../GitStoreMeta/ErrorCode.h"

namespace gitstore {
template<typename T>
struct has_set_allocated_code{
    template<typename U, void (U::*)(GitStoreGrpc::ErrorCode*)> struct HELPS;
    template<typename U> static char Test(HELPS<U, &U::set_allocated_code>*);
    template<typename U> static int Test(...);
    const static bool Has = sizeof(Test<T>(0)) == sizeof(char);
};

template <typename T>
void formatResponseErrorCode(T* response, ErrorStatus res) {
    static_assert (has_set_allocated_code<T>::Has, "Response Not Contain ErrorCode Member");
    auto error_code = new GitStoreGrpc::ErrorCode();
    error_code->set_msg(CErrorCodeManager::getInstance()->getErrorMessage(res));
    error_code->set_code(res);
    response->set_allocated_code(error_code);
}
}

#endif
